package com.jic.member.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MemberFavorite;
import com.jic.member.request.MemberFavoriteRequest;
import com.jic.member.response.MemberFavoriteResponse;
import com.jic.member.service.MemberFavoriteService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: 会员收藏后台管理系统的服务
 */
@RestController
@RequestMapping("/memberFavorite")
public class MemberFavoriteController  {

    @Autowired
    MemberFavoriteService memberFavoriteService;


    /*
    * 分页查询
    * */
    @ApiOperation(value = "获取所有会员收藏")
    @ResponseBody
    @RequestMapping("/queryList")
    public RestResult<PageResult<MemberFavoriteResponse>> queryList(@RequestBody MemberFavoriteRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MemberFavoriteRequest memberFavoriteRequest = BeanUtil.copy(form, MemberFavoriteRequest.class);
        PageResult<MemberFavorite> result = memberFavoriteService.queryPage(memberFavoriteRequest, page);
        List<MemberFavoriteResponse> memberFavoriteResponseList = result.getRows().stream().map(temp -> {
            MemberFavoriteResponse memberFavoriteResponse = BeanUtil.copy(temp, MemberFavoriteResponse.class);
            return memberFavoriteResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MemberFavoriteResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberFavoriteResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }
}
